<template>
  <div class="register-page-wrapper">
    <!-- 背景遮罩层 -->
    <div class="modal-overlay" v-if="showModal" @click="handleClose">
      <div class="modal-content" @click.stop>
        <div class="modal-header">
          <h2 class="title">AI代码生成平台 -- 用户注册</h2>
          <button class="close-btn" @click="handleClose">×</button>
        </div>
        <div class="modal-body">
          <div class="desc">不写一行代码，生成完整应用</div>
          <a-form :model="formState" name="basic" autocomplete="off" @finish="handleSubmit">
            <a-form-item name="userAccount" :rules="[{ required: true, message: '请输入账号' }]">
              <a-input v-model:value="formState.userAccount" placeholder="请输入账号" />
            </a-form-item>
            <a-form-item
              name="userPassword"
              :rules="[
                { required: true, message: '请输入密码' },
                { min: 8, message: '密码不能小于 8 位' },
              ]"
            >
              <a-input-password v-model:value="formState.userPassword" placeholder="请输入密码" />
            </a-form-item>
            <a-form-item
              name="checkPassword"
              :rules="[
                { required: true, message: '请确认密码' },
                { min: 8, message: '密码不能小于 8 位' },
                { validator: validateCheckPassword },
              ]"
            >
              <a-input-password v-model:value="formState.checkPassword" placeholder="请确认密码" />
            </a-form-item>
            <div class="tips">
              已有账号？
              <RouterLink to="/user/login">去登录</RouterLink>
            </div>
            <a-form-item>
              <a-button type="primary" html-type="submit" style="width: 100%">注册</a-button>
            </a-form-item>
          </a-form>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { useRouter } from 'vue-router'
import { userRegister } from '@/api/userController.ts'
import { message } from 'ant-design-vue'
import { reactive, ref, onMounted, onUnmounted } from 'vue'

const showModal = ref(true)
const router = useRouter()

const formState = reactive<API.UserRegisterRequest>({
  userAccount: '',
  userPassword: '',
  checkPassword: '',
})

/**
 * 验证确认密码
 * @param rule
 * @param value
 * @param callback
 */
const validateCheckPassword = (rule: unknown, value: string, callback: (error?: Error) => void) => {
  if (value && value !== formState.userPassword) {
    callback(new Error('两次输入密码不一致'))
  } else {
    callback()
  }
}

/**
 * 提交表单
 * @param values
 */
const handleSubmit = async (values: API.UserRegisterRequest) => {
  try {
    const res = await userRegister(values)
    // 注册成功，跳转到登录页面
    if (res.data.code === 0) {
      message.success('注册成功')
      handleClose()
      router.push({
        path: '/user/login',
        replace: true,
      })
    } else {
      message.error('注册失败，' + res.data.message)
    }
  } catch (error) {
    message.error('注册请求失败')
  }
}

/**
 * 关闭模态框
 */
const handleClose = () => {
  showModal.value = false
  // 恢复背景滚动
  document.body.style.overflow = ''
  // 返回上一页
  router.go(-1)
}

// 组件挂载时禁止背景滚动
onMounted(() => {
  document.body.style.overflow = 'hidden'
})

// 组件卸载时恢复背景滚动
onUnmounted(() => {
  document.body.style.overflow = ''
})
</script>

<style scoped>
.register-page-wrapper {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 999;
}

.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
}

.modal-content {
  background: white;
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
  width: 360px;
  max-width: 90vw;
  max-height: 90vh;
  overflow-y: auto;
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 25px 10px 5px 40px;
}

.close-btn {
  background: none;
  border: none;
  font-size: 24px;
  cursor: pointer;
  color: #999;
  padding: 0;
  width: 30px;
  height: 30px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
}

.close-btn:hover {
  color: #333;
  background-color: #f5f5f5;
}

.modal-body {
  padding: 0 24px 24px 24px;
}

.title {
  color: #9eb0b0;
  margin-bottom: 16px;
  margin-top: 0;
}

.desc {
  text-align: center;
  color: #ccc;
  margin-bottom: 16px;
}

.tips {
  margin-bottom: 16px;
  color: #919494;
  font-size: 13px;
  text-align: right;
}
</style>
